﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;

namespace WinFormsApp1.TongShiWei.Log
{
    public partial class LogData_ : UIForm
    {
        public LogData_()
        {
            InitializeComponent();
            GetLogData_();
        }
        int ynum = 1;
        int qnum = 10;
        int count = 0;
        int znum = 0;
        public async void GetLogData_()
        {
            List<LogData> list = new List<LogData>();
            //http://localhost:5159/api/TSW/ShowLogData
            var res = await HttpClientHelper.RequestUrl<LogData, ApiResult<List<LogData>>>(URLCommon.Url2, "/TSW/ShowLogData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                count = res.Data.Count();
                znum = Convert.ToInt32(Math.Ceiling((double)count / qnum));
                if (this.uiTextBox1.Text != "")
                {
                    res.Data = res.Data.Where(x => x.LogDatas.Contains(this.uiTextBox1.Text)).ToList();
                }
                res.Data = res.Data.OrderBy(x => x.LogId).Skip((ynum - 1) * qnum).Take(qnum).ToList();
                foreach (var item in res.Data)
                {
                    this.dataGridView1.Rows.Add(item.LogId, item.LogDatas, item.LogTime, item.LogUserName);
                }
            }
            this.Zcount.Text = count.ToString();
            this.Zynum.Text = znum.ToString();
        }
        public class LogData
        {
            /// <summary>
            /// 日志ID
            /// </summary>
            public int LogId { get; set; }
            /// <summary>
            /// 日志记录内容
            /// </summary>
            public string LogDatas { get; set; } = string.Empty;
            /// <summary>
            /// 日志记录时间
            /// </summary>
            public DateTime LogTime { get; set; }
            /// <summary>
            /// 操作人
            /// </summary>
            public string LogUserName { get; set; } = string.Empty;

        }

        private void uiButton4_Click(object sender, EventArgs e)
        {
            if (ynum < 2)
            {
                MessageBox.Show("已经是第一页了");
            }
            else
            {
                ynum--;
                this.dataGridView1.Rows.Clear();
                GetLogData_();
            }
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            if (ynum == znum)
            {
                MessageBox.Show("已经是最后一页了");
            }
            else
            {
                ynum++;
                this.dataGridView1.Rows.Clear();
                GetLogData_();
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            GetLogData_();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
